package reflect;

import reflect.annotations.AutoRunClass;
import reflect.annotations.AutoRunMethod;

import java.io.File;
import java.lang.reflect.Method;

public class Test7 {
    public static void main(String[] args) throws Exception{
        String packName = Text6.class.getPackage().getName();
        File file = new File(
                Text6.class.getResource(".").toURI()
        );
        for (File sub : file.listFiles(f ->f.getName().endsWith(".class"))){
            Class cls = Class.forName(packName + "." + sub.getName().replace(".class",""));
            if ( cls.isAnnotationPresent(AutoRunClass.class)) {
                Object obj = cls.newInstance();
                for (Method method : cls.getDeclaredMethods()){
                    if (method.isAnnotationPresent(AutoRunMethod.class)
                    ){
                        AutoRunMethod arm = method.getAnnotation(AutoRunMethod.class);
                        int value = arm.value();
                        for ( ;value > 0; value--) {
                            method.invoke(obj);
                        }

                    }
                }
            }
        }
    }
}
